package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.domain.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 暴露接口供app-server调用
 */
@DubboService
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据手机号查询用户
     *
     * @param mobile
     * @return
     */
    @Override
    public User findByMobile(String mobile) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(mobile != null, User::getMobile, mobile);
        return userMapper.selectOne(lqw);
    }
    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @Override
    public User getByID(Long id) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, User::getId, id);
        return userMapper.selectOne(lqw);
    }

    /**
     * 保存用户信息，并返回用户id
     *
     * @param user
     * @return
     */
    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    /**
     * 更新用户信息
     * @param user
     */
    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    /**
     * 根据环信账号查询用户
     * @param hxId
     * @return
     */
    @Override
    public User getByHxID(String hxId) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(hxId != null, User::getHxUser, hxId);
        User user = userMapper.selectOne(lqw);
        return user;
    }

}
